package com.eric.controller;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.eric.service.IUserRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 用户_角色表 前端控制器
 * </p>
 *
 * @author 新哥
 * @since 2024-03-12
 */
@RestController
@RequestMapping("/user-role")
@RequiredArgsConstructor
public class UserRoleController {
    private final IUserRoleService userRoleService;

    //    新增UserRole角色
    @PostMapping("/saveUserRole")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin"},mode = SaMode.OR)
    public SaResult saveUserRole(@RequestBody Map<String, List<Integer>> paramMaps){
        if (BeanUtil.isEmpty(paramMaps))
        {
            return SaResult.error("输入错误，请检查后再输入！");
        }
        return userRoleService.saveUserRole(paramMaps);
    }
//    删除一个用户的1~n个UserRole角色

    @DeleteMapping("/deleteUserRole")
    @SaCheckPermission("sys:delete")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin"},mode = SaMode.OR)
    public SaResult deleteUserRoles(@RequestBody Map<String,List<Integer>>ids){
        if (ids.isEmpty()){
            return SaResult.error("数据为空，请检查后再此提交");
        }
        return userRoleService.deleteUserRolesService(ids);
    }

    //    分页获取所有的信息
    @GetMapping("/getAll")
    public SaResult getAll(Integer current,Integer pageSize){
        return userRoleService.getAllService(current,pageSize);
    }

    //    通过userId获取所有数据
    @GetMapping("getByUserId")
    public SaResult getByUserid(Integer userId, Integer current,Integer pageSize){
        return userRoleService.getByUserIdService(userId,current,pageSize);
    }
    //    通过id获取信息
    @GetMapping("/{id}")
    public SaResult getOneById(@PathVariable("id") Integer id){
        return userRoleService.getOneByIdService(id);
    }

    @PutMapping("/updateUserRole")
    @SaCheckPermission("sys:modify")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin"},mode = SaMode.OR)
    @Transactional
    public SaResult updateUserRoles(@RequestBody Map<String, List<Integer>> paramMaps){
        return userRoleService.updateUserRoles(paramMaps);
    }


}
